package com.atguigu.mall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.common.constant.PmsConst;
import com.atguigu.common.page.PageFactory;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.mall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.mall.product.dao.AttrDao;
import com.atguigu.mall.product.entity.AttrEntity;
import com.atguigu.mall.product.service.AttrAttrgroupRelationService;
import com.atguigu.mall.product.service.AttrGroupService;
import com.atguigu.mall.product.service.AttrService;
import com.atguigu.mall.product.service.CategoryService;
import com.atguigu.mall.product.vo.AttrEntityVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service("attrService")
@Transactional(rollbackFor = Exception.class)
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrAttrgroupRelationDao attrgroupRelationDao;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private CategoryService  categoryService;
    @Autowired
    private AttrGroupService attrGroupService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<AttrEntity> page = PageFactory.defaultPage();
        List<AttrEntity> attrEntities = this.baseMapper.pageList(page, params);
        page.setRecords(attrEntities);
        return new PageUtils(page);
    }

    @Override
    public void saveAttrEntityVO(AttrEntityVO attrEntityVO) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtil.copyProperties(attrEntityVO,attrEntity);
        this.save(attrEntity);
        Long attrId = attrEntity.getAttrId();
        Integer attrType = attrEntity.getAttrType();
        /**
         * 基础属性 关联属性分组
         */
        if (PmsConst.ATTR_TYPE_BASE.equals(attrType)){
            Long attrGroupId = attrEntityVO.getAttrGroupId();
            attrAttrgroupRelationService.saveAttrAttrgroupRelation(attrId,attrGroupId);
        }
    }

    @Override
    public AttrEntityVO getAttrById(Long attrId) {
        AttrEntity attrEntity = this.getById(attrId);
        AttrEntityVO vo = new AttrEntityVO();
        BeanUtil.copyProperties(attrEntity,vo);
        /**
         * 查询出关联的三级分类
         */
        Long catelogId = attrEntity.getCatelogId();
        List<Long> catelogPath = categoryService.queryPathListById(catelogId);
        vo.setCategoryPath(catelogPath);
        /**
         * 查询出关联分组
         */
        Integer attrType = attrEntity.getAttrType();
        if (PmsConst.ATTR_TYPE_BASE.equals(attrType)){
            Long attrGroupIdByAttrId = attrgroupRelationDao.getAttrGroupIdByAttrId(attrId);
            vo.setAttrGroupId(attrGroupIdByAttrId);
        }
        return vo;
    }

    @Override
    public void updateAttrEntityVO(AttrEntityVO attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtil.copyProperties(attr,attrEntity);
        this.baseMapper.updateById(attrEntity);
        Long attrId = attr.getAttrId();
        Long attrGroupId = attr.getAttrGroupId();
        Integer attrType = attr.getAttrType();
        /**
         *  维护属性分组 ATTR_TYPE_BASE
         */
        if (PmsConst.ATTR_TYPE_BASE.equals(attrType)){
            attrAttrgroupRelationService.deleteByAttrId(attrId);
            attrAttrgroupRelationService.saveAttrAttrgroupRelation(attrId,attrGroupId);
        }
    }

    @Override
    public List<AttrEntity> queryListByCategoryId(Long categoryId) {
        LambdaQueryWrapper<AttrEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrEntity::getCatelogId,categoryId);
        List<AttrEntity> attrEntities = this.baseMapper.selectList(wrapper);
        return attrEntities;
    }

}